//
//  XVCAppointmentRecord.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/11.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCAppointmentRecord.h"
#import "XTCAppointmentInformationShow.h"
#import "XXTool.h"
#import "XVCAppontmentDetail.h"

@interface XVCAppointmentRecord ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableview;

@property (nonatomic) UILabel *noRecordLabel;

@end

@implementation XVCAppointmentRecord
{
    NSMutableArray <XMLMessageModel*> *useData;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(UILabel*)noRecordLabel
{
    if (_noRecordLabel==nil)
    {
        _noRecordLabel = [[UILabel alloc] init];
        _noRecordLabel.text = NSLocalizedString(@"暂无预约申请", nil);
        _noRecordLabel.font = [XXTool systemFontOfSize:16.5f];
        _noRecordLabel.textAlignment = NSTextAlignmentCenter;
        _noRecordLabel.textColor = [XXTool colorWithHexString:@"838383"];
        _noRecordLabel.backgroundColor = [UIColor clearColor];
        [_tableview addSubview:_noRecordLabel];
    
        _noRecordLabel.hidden = YES;
    }
    return _noRecordLabel;
}

-(void)initParameter
{

}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"预约记录", nil)];
}

-(void)initData
{
    [self.view startLoading];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    self.tableview.frame = CGRectMake(0, 0, frame.size.width
                                      , frame.size.height);
    self.noRecordLabel.frame = _tableview.frame;
}

-(void)getData
{
    [XMLMessageModel requestMessageListWithAcceptType:@"1"
                                            listCount:0
                                           completion:^(BOOL isSuccess, NSMutableArray<XMLMessageModel *> *dataList, NSInteger lookCount) {
                                               if (isSuccess)
                                               {
                                                   useData = dataList;
                                                   if (useData.count==0)
                                                   {
                                                       _noRecordLabel.hidden = NO;
                                                   }
                                                   else
                                                   {
                                                       _noRecordLabel.hidden = YES;
                                                   }
                                                   [_tableview reloadData];
                                               }
                                               [self.view stopLoading];
                                               [_tableview.mj_header endRefreshing];
                                           }];
}

-(void)footerFresh
{
    [XMLMessageModel requestMessageListWithAcceptType:@"1"
                                            listCount:useData.count
                                           completion:^(BOOL isSuccess, NSMutableArray<XMLMessageModel *> *dataList, NSInteger lookCount) {
                                               if (isSuccess)
                                               {
                                                   if (dataList.count==0)
                                                   {
                                                       //[SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多消息", nil)];
                                                   }
                                                   else
                                                   {
                                                       [useData addObjectsFromArray:dataList];
                                                       [_tableview reloadData];
                                                   }
                                               }
                                               [_tableview.mj_footer endRefreshing];

                                           }];
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTCAppointmentInformationShow *cell = [XTCAppointmentInformationShow cellSelectionStyleNoneWithTableView:tableView];
    [cell setUseType:XTCAppointmentInformationShowRecord];
    [cell setCellWithData:useData[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [XTCAppointmentInformationShow heightForCell];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.count;
}

#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMLMessageModel *temp = useData[indexPath.row];
    [XVCAppontmentDetail pushAppontmentDetailVCWithSourceVC:self apPersonID:temp.orderid completion:nil];
}

#pragma mark api
+(void)pushAppointmentRecordVCWithSourceVC:(UIViewController *)sourceVC
{
    XVCAppointmentRecord *vc = [[XVCAppointmentRecord alloc] init];
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
